package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@WebServlet("/hello")
public class HelloWord extends HttpServlet{
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Log.getLogger(Server.class);
	private static String showContent;
	private static String uploadEncoding;
	/**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWord() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("hello word!");
		request.setCharacterEncoding(uploadEncoding);
		response.setCharacterEncoding(uploadEncoding);
		response.setContentType("text/html;charset="+uploadEncoding);
		
		getHtmlContent(request.getContextPath());
		String msg = request.getParameter("msg");
		if(msg!=null){
			logger.info(msg);
		}
		if(msg!=null&&!"".equals(msg)){
			showContent = showContent.replace("${msg}", msg);
		}else{
			showContent = showContent.replace("${msg}", "");
		}
		PrintWriter writer = response.getWriter();
		writer.write(showContent);
		writer.flush();
		writer.close();
	}
	private static void getHtmlContent(String contextPath) {
		showContent = "<html><head><meta charset=\""+uploadEncoding+"\"/><title>Upload file</title></head><body><h2>Upload file</h2><hr/><br>"
				+ "<form action=\""+contextPath+"/UploadHandlerServlet\" enctype=\"multipart/form-data\" method=\"post\" ><input type=\"file\" name=\"File1\" size=\"20\" maxlength=\"20\"><input type=\"submit\"value=\"Upload\"></form><br/>"
				+ "<p style=\"color: green;\">${msg}</p>"
				+ "</body></html>\n";
	}
	static{
		uploadEncoding = "utf-8";
	}
}
